import React, { useEffect, useState } from 'react'
import PropTypes from 'prop-types'
import { Row, Col } from 'antd'
import moment from 'moment'
import style from './InfoBlock.module.less'
import { acceptStatusList, normalStatusList } from '@js/config'
import { $get } from '@js/authRequest'
import { awaitWrap, strToNum } from '@js/tool'

// 格式化钱
function formatMoney (num = 0) {
  return String(num || 0).replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ' 元'
}

// 渲染仲裁员
function getArbitrator (d) {
  const { arbitrator, arbitrator1, arbitrator2 } = d || {}
  const result = [arbitrator, arbitrator1, arbitrator2].filter(i => i)
  return result.join('、') || '无'
}

// 获取收案阶段默认显示样式
function getAcceptDefaultStatus (d, isAll) {
  const s = acceptStatusList.indexOf(d.status) !== -1 || isAll
  if (!s) {
    return { display: 'none' }
  }
}

// 获取立案后默认显示样式
function getBusinessDefaultStatus (d, isAll) {
  const s = normalStatusList.indexOf(d.status) !== -1 || isAll
  if (!s) {
    return { display: 'none' }
  }
}

// 获取默认状态显示样式
// function getNormalDefaultStatus (isAll) {
//   if (!isAll) {
//     return { display: 'none' }
//   }/*  */
// }

async function getCalculateTrialLimitRequest (caseid) {
  const res = await $get('/api/xapc/case/calculateTrialLimit', {
    caseid
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取审限届满之日
async function getCalculateTrialLimit (caseid, setTrialLimit) {
  const [e, d] = await awaitWrap(getCalculateTrialLimitRequest(caseid))
  if (e === null && strToNum(d)) {
    setTrialLimit(moment(strToNum(d) * 1000).format('YYYY年MM月DD日'))
  }
}

function Main ({ caseDetail: d, isAll }) {
  const [trialLimit, setTrialLimit] = useState('')
  useEffect(() => {
    if (d !== null) {
      getCalculateTrialLimit(d.id, setTrialLimit)
    }
  }, [d])
  if (!d) {
    return <span />
  }
  // const claim = d.claim || {}
  return (
    <div className={style.container}>
      <Row>
        <Col span={8}>
          <span className={style.cell}>
            <span className={style['cell-label']}>案由：</span>
            <span className={style['cell-text']}>
              {d.cause_action || '无'}
              {d.cause_action_ext ? `，${d.cause_action_ext}` : null}
            </span>
          </span>
        </Col>
        <Col span={8}>
          <span className={style.cell}>
            <span className={style['cell-label']}>仲裁程序：</span>
            <span className={style['cell-text']}>{d.procedure || '无'}</span>
          </span>
        </Col>
        <Col span={8}>
          <span className={style.cell}>
            <span className={style['cell-label']}>争议金额：</span>
            <span className={style['cell-text']}>{formatMoney(d.dispute_amount)}</span>
          </span>
        </Col>
        <Col span={8}>
          <span className={style.cell}>
            <span className={style['cell-label']}>仲裁费：</span>
            <span className={style['cell-text']}>{formatMoney(d.fee)}</span>
          </span>
        </Col>
        <Col span={8} style={getAcceptDefaultStatus(d, isAll)}>
          <span className={style.cell}>
            <span className={style['cell-label']}>受理费：</span>
            <span className={style['cell-text']}>{formatMoney(d.fee_acceptance)}</span>
          </span>
        </Col>
        <Col span={8} style={getAcceptDefaultStatus(d, isAll)}>
          <span className={style.cell}>
            <span className={style['cell-label']}>处理费：</span>
            <span className={style['cell-text']}>{formatMoney(d.fee_processing)}</span>
          </span>
        </Col>
        {/* <Col span={8} style={getAcceptDefaultStatus(d, isAll)}>
          <span className={style.cell}>
            <span className={style['cell-label']}>收费方式：</span>
            <span className={style['cell-text']}>{claim.fee_type || '无'}</span>
          </span>
        </Col> */}
        <Col span={8} style={getBusinessDefaultStatus(d, isAll)}>
          <span className={style.cell}>
            <span className={style['cell-label']}>仲裁员：</span>
            <span className={style['cell-text']}>{getArbitrator(d)}</span>
          </span>
        </Col>
        <Col span={8} style={getBusinessDefaultStatus(d, isAll)}>
          <span className={style.cell}>
            <span className={style['cell-label']}>办案秘书：</span>
            <span className={style['cell-text']}>{d.processer || '无'}</span>
          </span>
        </Col>
        {/* <Col span={8} style={getNormalDefaultStatus(isAll)}>
          <span className={style.cell}>
            <span className={style['cell-label']}>案件来源：</span>
            <span className={style['cell-text']}>{d.accept_structure || '无'}</span>
          </span>
        </Col> */}
        {d.closure_datetime ? (
          <Col span={8}>
            <span className={style.cell}>
              <span className={style['cell-label']}>结案方式：</span>
              <span className={style['cell-text']}>{d.closure_type || '无'}</span>
            </span>
          </Col>
        ) : null}
        {trialLimit ? (
          <Col span={8}>
            <span className={style.cell}>
              <span className={style['cell-label']}>审限至：</span>
              <span className={style['cell-text']}>{trialLimit || '无'}</span>
            </span>
          </Col>
        ) : null}
        <Col span={8}>
          <span className={style.cell}>
            <span className={style['cell-label']}>案件标签：</span>
            <span className={style['cell-text']}>{d.user_tag || '无'}</span>
          </span>
        </Col>
      </Row>
    </div>
  )
}

Main.propTypes = {
  caseDetail: PropTypes.object,
  isAll: PropTypes.bool
}

Main.defaultProps = {
  caseDetail: {},
  isAll: false
}

export default Main
